/**
 * 后台接口
 * @type {{msg: {}, role: {}, dict: {}, menu: {}, user: {}}}
 */
top.API = {
    activiti:()=>{
        function suspend(data,callback){
            top.API.base('/activiti/suspend',data,callback);
        }
        function activate(data,callback){
            top.API.base("/activiti/activate",data,callback)
        }
        function deploy(data,callback){
            top.API.base('/activiti/deploy',data,callback);
        }
        function edit(data,callback){
            top.API.base("/activiti/edit",data,callback)
        }
        function deleteById(data,callback){
            top.API.base("/activiti/deleteDeploymentById",data,callback)
        }
        function startProcess(data,callback){
            top.API.base("/activiti/startProcess",data,callback)
        }
        function completeTask(data,callback){
            top.API.base("/activiti/completeTask",data,callback)
        }
        function verify(data,callback){
            top.tools.ajax({
                url: "/activiti/verify",
                loading:false,
                type: 'POST',
                dataType: 'json',
                data: data,
                successFun:callback
            })
        }
        return {completeTask,startProcess,activate,suspend,deleteById,deploy,edit,verify}
    },
    common:()=>{
        function  loadPage(url,callback){
            top.tools.ajax({
                url: "/webPageLoader",
                type: "POST",
                headers: {
                    'page': url
                },
                data: {pageUrl:  url},
                async: false,
                successFun: function(response,status,xhr){
                    callback(response);
                },
                dataType: "text"
            })
        }
        return {
            loadPage
        }
    },
    ssl:()=>{
        function save(data,callback){
            if (data.id){
                top.API.base('/ssl/save',data,callback);
            }else {
                top.API.base('/ssl/add',data,callback);
            }
        }
        function del(data,callback){
            top.API.base('/ssl/del',data,callback);
        }
        function apply(data,callback){
            top.API.base('/ssl/apply',data,callback);
        }
        function renew(data,callback){
            top.API.base('/ssl/renew',data,callback);
        }
        return {
            save,
            del,
            apply,
            renew
        }
    },
    email:()=>{
        function getAllMailAccount(data,callback){
            top.API.base('/email/account/all',data,callback);
        }
        function mailAccountSave(data,callback){
            if (data.id){
                top.API.base('/email/account/save',data,callback);
            }else {
                top.API.base('/email/account/add',data,callback);
            }
        }
        function mailAccountDel(data,callback){
            top.API.base('/email/account/del',data,callback);
        }
        function mailTemplateSave(data,callback){
            if (data.id){
                top.API.base('/email/template/save',data,callback);
            }else {
                top.API.base('/email/template/add',data,callback);
            }
        }
        function mailTemplateDel(data,callback){
            top.API.base('/email/template/del',data,callback);
        }

        return {
            getAllMailAccount,
            mailAccountSave,
            mailAccountDel,
            mailTemplateSave,
            mailTemplateDel
        }
    },
    user:()=>{
        function save(data,callback){
            if (data.id){
                top.API.base('/user/updateUser',data,callback);
            }else {
                userVerify(data,(res)=>{
                    if (res.data && res.data>0){
                        layer.msg(res.message, {icon: 0});
                    }else {
                        top.API.base('/user/addUser',data,callback);
                    }
                })
            }
        }
        function delUser(data,callback){
            top.API.base('/user/delUser',data,callback);
        }
        function userVerify(data,callback){
            top.API.base('/user/userVerify',data,callback);
        }
        function updatePassword(data,callback){
            top.API.base('/user/updatePassword',data,callback);
        }
        function bindRole(data,callback){
            top.API.base("/user/bindRole",data,callback)
        }
        function getRole(data,callback){
            top.API.base("/user/getRole",data,callback)
        }
        return {
            save,delUser,updatePassword,bindRole,getRole
        }
    },
    role:()=>{
        function save(data,callback){
            if (data.id){
                top.API.base("/role/saveRole",data,callback)
            }else {
                top.API.base("/role/addRole",data,callback)
            }
        }
        function del(data,callback){
            top.API.base("/role/delRole",data,callback)
        }
        function bindResources(data,callback){
            top.API.base("/role/bindResources",data,callback)
        }
        function getResources(data,callback){
            top.API.base("/role/getResources",data,callback)
        }
        return {
            save,
            del,
            bindResources,
            getResources
        }
    },
    dict:()=>{
        function save(data,callback){
            if (data.id){
                top.API.base("/dict/saveDictType",data,callback)
            }else {
                top.API.base("/dict/addDictType",data,callback)
            }
        }
        function del(data,callback){
            top.API.base("/dict/delDictType",data,callback)
        }
        function dictDataSave(data,callback){
            if (data.id){
                top.API.base("/dict/saveDictData",data,callback)
            }else {
                top.API.base("/dict/addDictData",data,callback)
            }
        }
        function dictDataRemove(data,callback){
            top.API.base("/dict/delDictData",data,callback)
        }
        return {
            save,
            del,
            dictDataSave,
            dictDataRemove
        }
    },
    menu:()=>{
        function status(data,callback){
            top.API.base("/menu/status",data,callback)
        }
        function save(data,callback){
            if (data.id){
                top.API.base("/menu/saveMenu",data,callback)
            }else {
                top.API.base("/menu/addMenu",data,callback)
            }
        }
        function del(data,callback){
            top.API.base("/menu/delMenu",data,callback)
        }
        function getListById(data,callback){
            top.tools.ajax({
                url: "/menu/getListById",
                loading:false,
                type: 'POST',
                dataType: 'json',
                data: data,
                successFun:callback
            })
        }
        function getResources(data,callback){
            if (data.roleId){
                top.API.base("/role/getResources",{id:data.roleId},callback)
            }
            if (data.userId){
                top.API.base("/user/getResources",{id:data.userId},callback)
            }
        }
        function getAllParentResources(data,callback){
            top.API.base("/menu/getAllParentResources",data,callback)
        }
        return {
            status,
            save,
            del,
            getListById,
            getResources,
            getAllParentResources
        }
    },
    task:()=>{
        function save(data,callback){
            if (data.id){
                top.API.base("/scheduled/task/save",data,callback)
            }else {
                top.API.base("/scheduled/task/add",data,callback)
            }
        }
        function del(data,callback){
            top.API.base("/scheduled/task/del",data,callback)
        }
        function execute(data,callback){
            top.API.base("/scheduled/task/execute",data,callback)
        }
        function log(data,callback){
            top.API.base("/scheduled/task/log",data,callback)
        }
        function logs(data,callback){
            top.API.base("/scheduled/task/logs",data,callback)
        }
        function countStatus(data,callback){
            top.tools.ajax({
                url: "/scheduled/task/countStatus",
                type: 'POST',
                dataType: 'json',
                loading:false,
                data: data,
                successFun:callback
            })
        }
        return {
            save,
            del,
            execute,
            logs,
            log,
            countStatus
        }
    },
    //消息模块
    msg:()=>{
        //查看未读消息
        function getUnRead(callback) {
            top.API.base("",{status:0},callback)
        }
        //返回消息列表
        function getAllRead (data, callback) {
            top.API.base("/message/getAllMsg",data,callback)
        }
        // 查看指定消息
        function getMsgById(id,callback){
            top.API.base("",{id:id},callback)
        }
        return {
            getAllRead,
            getUnRead,
            getMsgById
        }
    },
    base:(url,data,callback)=>{
        top.tools.ajax({
            url: url,
            type: 'POST',
            dataType: 'json',
            data: data,
            successFun:callback
        })
    }
}